#include "../inc/touch.h"

int open_touch(){
    //1 打开触摸屏设备文件  touch sreen
    tsFd = open("/dev/input/event0", O_RDWR);//这里多加了int导致查错了十几分钟
    if(tsFd == -1)
    {
        perror("open touch error");
        return -1;
    }
}

void close_touch(){
    //4 关闭触摸屏文件
    close(tsFd);
}

int touch_wait(int *x, int *y)
{
    //2 读取触摸屏设备的坐标
    while(1)
    {
        struct input_event info;
        read(tsFd,&info,sizeof(info));
        //判断是触摸屏事件 而且是X轴事件
        if(info.type == EV_ABS && info.code == ABS_X)
        {
            *x = info.value;
        }
        //判断是触摸屏事件 而且是Y轴事件
        if(info.type == EV_ABS && info.code == ABS_Y)
        {
            *y = info.value;
        }
        //3 将触摸屏的坐标打印出来
        //printf("type:%d, code:%d, value:%d\n", info.type, info.code, info.value);
        //压力事件 并且是触摸屏的压力事件，松开的时候
        if(info.type == EV_KEY && info.code == BTN_TOUCH && info.value == 0)
        {
            //黑色屏幕的开发板 坐标范围：x(0-1024)   y(0-600)
            //蓝色屏幕的开发板 坐标范围：x(0-800)   y(0-480)
            *x =  ((*x) * 800 *1.0) / 1024;
            *y =  ((*y) * 480 *1.0) / 600;
            printf("x = %d, y = %d\n", *x, *y);   
            break;        
        }
    }

}

enum WARD touch_wait_slide(){
    int firstX = 0,firstY = 0,afterX = 0,afterY = 0,finalX = 0,finalY = 0;
    int flag = 0;
    int test = 0;
    //1 打开触摸屏设备文件  touch sreen
    //int tsFd = open("/dev/input/event0", O_RDWR);
    // if(tsFd == -1)
    // {
    //     perror("open touch error");
    //     return -1;
    // }
    //2 读取触摸屏设备的坐标
    while(1){
        struct input_event info;
        read(tsFd,&info,sizeof(info));
        

        if(flag == 0){
            if(info.type == EV_ABS && info.code == ABS_X){
                firstX = info.value;
                // printf("firstx\n");
                flag = 1;
            }
        }
        if(flag == 1){
            if(info.type == EV_ABS && info.code == ABS_Y){
                firstY = info.value;
                // printf("firsty\n");
                flag = 2;
            }
        }
        if(info.type == EV_ABS && info.code == ABS_X){
            afterX = info.value;
            // printf("afterx\n");
            // flag = 3;
        }
        
        if(info.type == EV_ABS && info.code == ABS_Y){
            afterY = info.value;
            // printf("aftery\n");
        }
        if(info.type == EV_KEY && info.code == BTN_TOUCH && info.value == 0){
            break;
        }
    }
    finalX = afterX - firstX;
    finalY = afterY - firstY;
    // printf("firstX:%d  firstY:%d  afterX:%d  afterY:%d  finalX:%d  finalY:%d\n",firstX,firstY,afterX,afterY,finalX,finalY);
    // if(finalX == 0 && finalY == 0)
    //     return TOUCH;

    if(finalX > 0){
        if(finalY > 0){
            if(finalX > finalY){
                return RIGHT;
            }else{
                return DOWN;
            }
        }else{
            if(finalX > abs(finalY)){
                return RIGHT;
            }else{
                return UP;
            }
        }
    }else{
        if(finalY > 0){
            if(abs(finalX) > finalY){
                return LEFT;
            }else{
                return DOWN;
            }
        }else{
            if(abs(finalX) > abs(finalY)){
                return LEFT;
            }else{
                return UP;
            }
        }
    }
    
    // //4 关闭触摸屏文件
    // close(tsFd);
}